package edu.xlh.travel.service.impl;

import edu.xlh.travel.common.Result;
import edu.xlh.travel.dao.EmailDao;
import edu.xlh.travel.dao.ForeHotelOrderDao;
import edu.xlh.travel.enumeration.OrderTypeEnum;
import edu.xlh.travel.pojo.Email;
import edu.xlh.travel.pojo.Hotel_orders;
import edu.xlh.travel.pojo.User;
import edu.xlh.travel.service.EmailService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpSession;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class EmailServiceImpl implements EmailService {
    @Autowired
    private EmailDao emailDao;
    @Autowired
    private ForeHotelOrderDao foreHotelOrderDao;
    @Override
    public Result getRecentHotelEmail(HttpSession session) {
        User user = (User)session.getAttribute("user");
        Specification specification = new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> emailList = new ArrayList<>();
                emailList.add(criteriaBuilder.equal(root.get("receiveUserId") , user.getId()));
                emailList.add(criteriaBuilder.equal(root.get("receiveUserId") , user.getId()));
                emailList.add(criteriaBuilder.equal(root.get("orderType") , OrderTypeEnum.hotelOrder.getCode()));
                emailList.add(criteriaBuilder.equal(root.get("delFlag") , "0"));
                return criteriaBuilder.and(emailList.toArray(new Predicate[emailList.size()]));
            }
        };
        Sort sort = Sort.by(Sort.Direction.DESC, "sendTime");
        List<Email> emailList  = emailDao.findAll(specification , sort);
        return new Result(0 , emailList);
    }

    @Override
    public Result getRecentScenicEmail(HttpSession session) {
        User user = (User)session.getAttribute("user");
        Specification specification = new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> emailList = new ArrayList<>();
                emailList.add(criteriaBuilder.equal(root.get("receiveUserId") , user.getId()));
                emailList.add(criteriaBuilder.equal(root.get("receiveUserId") , user.getId()));
                emailList.add(criteriaBuilder.equal(root.get("orderType") , OrderTypeEnum.scenicOrder.getCode()));
                emailList.add(criteriaBuilder.equal(root.get("delFlag") , "0"));
                return criteriaBuilder.and(emailList.toArray(new Predicate[emailList.size()]));
            }
        };
        Sort sort = Sort.by(Sort.Direction.DESC, "sendTime");
        List<Email> emailList  = emailDao.findAll(specification , sort);
        return new Result(0 , emailList);
    }

    @Override
    public Result setIsRead(String id , HttpSession session) {
        emailDao.updateIsReadById(id , "1");
        return null;
    }
}
